package com.wikidot.designpatterns.composite.alternative2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * This alternative of composite pattern implements helper methods (for
 * manipulation of composite structure) within abstract superclass. This allows
 * to inherit this behavior in all descendants and easier usage when adding
 * elements into composite structure (no class casting is necessary). On the
 * other hand, no additional extension by sub-classing is possible.
 */
public abstract class Component {
	protected Collection<Component> composite = new ArrayList<Component>();
	protected String name;
	
	public Component(String name) {
		this.name = name;
	}

	/*
	 * Composite management logic - methods for manipulating composite structure
	 * (adding, removing, listing, ...). All inherited classes will dispose
	 * these methods - even leaf elements.
	 */
	public final void addComponent(Component c) {
		composite.add(c);
	}

	public final void removeComponent(Component c) {
		composite.remove(c);
	}

	public final Iterator<Component> listComponents() {
		return composite.iterator();
	}

	/*
	 * Business logic - declaration (implementation is also possible) of
	 * business operations. These methods might vary across inheritance
	 * hierarchy.
	 */
	public abstract void businessOperation();
}
